package com.apes.fn.fico.reimbursement.model;

import com.apes.fico.masterdata.gl.model.AccountingChart;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.AccessProvider;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * 报销费用类型
 * Created by lishikun on 2021/1/27 17:46
 */
@Getter
@Setter
@SelectBeforeUpdate @DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FeeType.CONST_TABLE_NAME)
public class FeeType extends Model {

    public static final String CONST_TABLE_NAME = "fn_fee_type";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "费用类型表", padding = 8)
    @Comment("费用类型编码")
    private String id;

    @Column(name = "name")
    @Comment("费用名称")
    private String name;

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "expense", referencedColumnName = "id")
    private Enumeration expense;

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "subject_name_id", referencedColumnName = "id")
    @Comment("科目名称")
    private AccountingChart subjectName;

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "withholding_subject_id", referencedColumnName = "id")
    @Comment("预提科目")
    private AccountingChart withholdingSubject;

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "pending_subject_id", referencedColumnName = "id")
    @Comment("待摊科目")
    private AccountingChart pendingSubject;

    @Column(name = "withholding_subject_mark", nullable = false, columnDefinition =  ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("预提标记")
    private Boolean withholdingSubjectMark = false;

    @Column(name = "pending_subject_mark", nullable = false, columnDefinition =  ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("待摊标记")
    private Boolean pendingSubjectMark = false;

    @Column(name = "type", nullable = false, columnDefinition =  ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("类型：0（门店），1（总部），2（其他）")
    private int type;

    @Column(name = "reimbursement_mark", nullable = false, columnDefinition =  ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("报销标记")
    private Boolean reimbursementMark = false;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "access_provider_id", referencedColumnName = "id")
    @Comment("接入商")
    private AccessProvider accessProvider;

    @Column(name = "valid", nullable = false, columnDefinition = "number(1, 0) default 1")
    @Comment("有效标记")
    private boolean valid;

    //前端展示名称 费用类型名称+类型
    @Transient
    private String typeName;

    public String getTypeName() {
        String type = this.type == 0 ? "门店" : this.type == 1 ? "总部" : "其他";
        return this.name + "—" + type;
    }


}
